//前台项目请求
//引入axios
import axios from "axios";
//引入获取token的函数
import { getToken } from "./token-utils";
import { ElMessage } from "element-plus";
import { v4 as uuidv4 } from 'uuid';
const request = axios.create({
  baseURL: "/api",
  timeout: 5000,
});
//用户标识符：【生成一次立马保存】   6e6ef6e4-7332-418e-9cde-74d62257d0cf
const getUserUUID = () => {
  //从本地存储获取唯一标识
  let userID: any = localStorage.getItem("USER_TEMPID");
  if (!userID) {
    //生成一个用户标识
    userID = uuidv4();
    localStorage.setItem("USER_TEMPID", userID);
  }
  return userID;
};

request.interceptors.request.use((config: any) => {
  config.headers.userTempId = getUserUUID();
  let token = getToken();
  if (token) {
    config.headers.token = token;
  }

  return config;
});

request.interceptors.response.use(
  /* 约束一下response */
  async (response) => {
    // 对响应数据做点什么
    // const res = response.data

    return response.data; /* 返回成功响应数据中的data属性数据 */
  },
  (error) => {
    // 对响应错误做点什么
    return Promise.reject(error);
  }
);
export default request;
